export const getText = (data, key) => data?.find(i => i.value === key)?.label;

// hex => rgb
export const hex2Rgb = hexColor => {
  const reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
  if (hexColor && reg.test(hexColor)) {
    const rgb = [];
    hexColor = hexColor.substr(1);
    if (hexColor.length === 3) {
      hexColor = hexColor.replace(/(.)/g, '$1$1');
    }
    hexColor.replace(/../g, function (color) {
      rgb.push(parseInt(color, 0x10));
    });
    return `rgb(${rgb.join(',')})`;
  }
  return hexColor;
};

// hex => rgba
export const hex2Rgba = (hexColor, opacity) => {
  const reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
  if (hexColor && reg.test(hexColor)) {
    const rgb = [];
    hexColor = hexColor.substr(1);
    if (hexColor.length === 3) {
      hexColor = hexColor.replace(/(.)/g, '$1$1');
    }
    hexColor.replace(/../g, function (color) {
      rgb.push(parseInt(color, 0x10));
    });
    return `rgb(${rgb.join(',')}, ${opacity})`;
  }
  return hexColor;
};

// rgb => hex
const rgb2Hex = rgbColor => {
  const color = rgbColor.toString().match(/\d+/g);
  let hex = '#';

  for (let i = 0; i < 3; i++) {
    hex += ('0' + Number(color[i]).toString(16)).slice(-2);
  }
  return hex;
};